package com.aijk.web.admin.controller.openapi;

import com.aijk.ehm.api.*;
import com.aijk.ehm.model.EhmBanner;
import com.aijk.ehm.model.EhmResident;
import com.aijk.ehm.model.SemStep;
import com.aijk.ehm.model.qo.EhmBannerQo;
import com.aijk.ehm.model.qo.SemStepQo;
import com.aijk.web.admin.controller.openapi.entity.ApiResponse;
import com.aijk.web.admin.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Component
@Controller
public class IndexController extends ApiBaseController {

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private SemStepApi stepApi;

    @Autowired
    private EhmBannerApi ehmBannerApi;

    @Autowired
    private EhmInfoClassifyApi ehmInfoClassifyApi;

    @Autowired
    private EhmInfoApi ehmInfoApi;

    @Autowired
    private EhmInfoSrcApi ehmInfoSrcApi;

    /**
     * 首页记步排名
     * @param resident
     * @return
     */
    @RequestMapping(path = "app/index/step", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object companyList(@Valid EhmResident resident) {
        if(StringUtils.isEmpty(resident.getCompanyId()))
            return  new ApiResponse("-1","参数异常");

        //个人记步排名
        SemStepQo stepQo= new SemStepQo();
        int countAll = 0;
        int countCompany = 0;
        stepQo.setStartdate(DateUtil.getDateTime(DateUtil.dateFormate(new Date(),"yyyy-MM-dd")+" 00:00:00"));
        stepQo.setEnddate(DateUtil.getDateTime(DateUtil.dateFormate(new Date(), "yyyy-MM-dd")+" 23:59:59"));
        stepQo.setResidentId(resident.getId());
        List<SemStep> residentStep = stepApi.list(stepQo);
        if (residentStep.size()>0 && residentStep != null){
            stepQo.setResidentId(null);
            stepQo.setStepNum(residentStep.get(0).getStepNum());
            countAll = stepApi.count(stepQo);
            stepQo.setCompanyId(resident.getCompanyId());
            countCompany = stepApi.count(stepQo);
        }

        HashMap map = new HashMap();
        map.put("stepAll",countAll);
        map.put("stepCompanys",countCompany);

        ApiResponse response = new ApiResponse();
        response.setData(map);
        return response;
    }

    /**
     * 首页轮播图
     * @param
     * @return
     */
    @RequestMapping(path = "app/index/banner", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Object bannerList() {
        //首页banner
        //测试环境
        String url = "http://jbzgjk.aijk.com/h5/banner/browse/";
        EhmBannerQo ehmBannerQo = new EhmBannerQo();
        ehmBannerQo.setStatus((short) 1);
        List<EhmBanner> banners = ehmBannerApi.list(ehmBannerQo);
        for (EhmBanner banner:banners){
            if (banner.getHrefType() == 1){
                String h5Url= url + banner.getId();
                banner.setHrefUrl(h5Url);
            }
        }

        HashMap map = new HashMap();
        map.put("banner",banners);
        ApiResponse response = new ApiResponse();
        response.setData(map);
        return response;
    }


}
